package com.nervenets.general.utils;


/**
 * 计算距离工具类
 *
 * @author xiaohua
 * @data 2021/11/19 0019 13:50
 */
public class AddressUtils {


    public static double distance(double long1, double lat1, double long2, double lat2) {
        double a, b, R;
        R = 6378137;
        lat1 = lat1 * Math.PI / 180.0;
        lat2 = lat2 * Math.PI / 180.0;
        a = lat1 - lat2;
        b = (long1 - long2) * Math.PI / 180.0;
        double d;
        double sa2, sb2;

        sa2 = Math.sin(a / 2.0);

        sb2 = Math.sin(b / 2.0);

        d = 2 * R * Math.asin(Math.sqrt(sa2 * sa2 + Math.cos(lat1) * Math.cos(lat2) * sb2 * sb2));
        return d;
    }

    /**
     * @param paramLongitudeFrom  (用户所在位置经度)
     * @param paramLatitudeFrom   (用户所在位置纬度)
     * @param targetLongitudeFrom (目标所在位置经度)
     * @param targetLatitudeFrom  (目标所在位置纬度)
     * @return
     */

    public static double sort(double paramLongitudeFrom, double paramLatitudeFrom, double targetLongitudeFrom, double targetLatitudeFrom) {
        if (paramLongitudeFrom > 0 && paramLatitudeFrom > 0) {
            double dis = distance(paramLongitudeFrom, paramLatitudeFrom, targetLongitudeFrom, targetLatitudeFrom);
            return dis;
        } else {
            return 0.0;
        }

    }


}
